package com.agricultural.machine.proxy;

import com.agricultural.machine.common.IMachine;
import com.agricultural.machine.factory.MachineFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 农机代理工厂类 - 代理模式
 */
public class MachineProxyFactory {
    private static MachineProxyFactory instance;
    private final Map<String, IMachineProxy> proxies;
    private final MachineFactory machineFactory;

    private MachineProxyFactory() {
        this.proxies = new HashMap<>();
        this.machineFactory = MachineFactory.getInstance();
    }

    public static synchronized MachineProxyFactory getInstance() {
        if (instance == null) {
            instance = new MachineProxyFactory();
        }
        return instance;
    }

    /**
     * 创建农机代理
     */
    public IMachineProxy createMachineProxy(String type) {
        // 创建新的机器实例
        IMachine machine = machineFactory.createMachine(type);
        // 使用机器ID作为唯一标识符
        String key = machine.getMachineId() + "_proxy";
        IMachineProxy proxy = new MachineProxy(machine);
        proxies.put(key, proxy);
        return proxy;
    }

    /**
     * 获取已创建的代理
     */
    public IMachineProxy getProxy(String machineId) {
        String key = machineId + "_proxy";
        return proxies.get(key);
    }

    /**
     * 清除所有代理
     */
    public void clearProxies() {
        proxies.clear();
    }
} 